﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Globalization;

namespace PerCiGal.Common
{
    public class FunctionWeb
    {
        public static WebClient GetWebClient()
        {
            var client = new WebClient();

            //Internet Explorer Setting
            var defaultProxy = WebProxy.GetDefaultProxy();
            defaultProxy.UseDefaultCredentials = true;
            client.Proxy = defaultProxy;

            return client;
        }

        public static string GetPage(string url)
        {
            try
            {
                WebClient webClient = GetWebClient();

                byte[] reqHTML = webClient.DownloadData(url);

                CultureInfo cultureInfo = CultureInfo.CurrentUICulture;
                int codePage = cultureInfo.TextInfo.ANSICodePage;
                Encoding encoding = codePage.Equals(0) ? Encoding.UTF8 : Encoding.GetEncoding(codePage);
                return encoding.GetString(reqHTML);
            }
            catch (WebException e)
            {
                throw e;
            }
            catch (Exception e)
            {
                throw;
            }
        }
    }
}
